import { Component, OnInit, ViewChild, TemplateRef, ChangeDetectorRef, ElementRef, ChangeDetectionStrategy, Input } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { format } from 'date-fns';
import { SFSchema, SFComponent, SFCheckboxWidgetSchema, WidgetRegistry } from '@delon/form';
import { SupDic, HttpService, GridComponent } from 'lbf';
import { STColumn, STComponent, STPage, STChange, STData } from '@delon/abc';

import { NzMessageService, NzModalService, NzModalRef, NzListItemComponent } from 'ng-zorro-antd';
import { Template } from '@angular/compiler/src/render3/r3_ast';
import { DatePipe } from '@angular/common';
import { Lbka03WidgetComponent } from '@shared';


@Component({
  selector: 'app-xtgn-jztemplate',
  templateUrl: './jztemplate.component.html',
  providers: [DatePipe],
})
export class XtgnJztemplateComponent implements OnInit {

  constructor(private lbservice: HttpService,
    public message: NzMessageService,
  ) {
  }

  @Input()
  record:any;

  dateRange = [];
  shown = true;
  expandForm = false;
  fundvisible = false;
  costvisible = false;
  drvisible = false;
  textvisible = false;
  popvisible = false;
  tplModal: NzModalRef;
  list: any[] = [null];
  inputValue: string;
  person: string;
  fundData = [];
  formData = [];

  loading = false;

  fundPieData: STData[] = [
    {
      x: '现金',
      y: 100
    },
    {
      x: '其他',
      y: 1231,
    },
  ]

  costPieData: STData[] = [
    {
      x: '现金',
      y: 100
    },
    {
      x: '其他',
      y: 1231,
    },
  ]
  data: any[] = []

  stpage: STPage = {
    total: true,
    show: true, // 显示分页
    front: false // true是前端分页，false后端控制分页
  };
  stchange: STChange = {
    type: "pi",
    pi: 1,
    ps: 10,
    total: 0,
  }
  fund = {
    total: `&yen ${this.fundPieData.reduce((pre, now) => now.y + pre, 0).toFixed(2)}`,
    format: (val: number) => {
      return `&yen ${val.toFixed(2)}`;
    },
  }
  cost = {
    total: `&yen ${this.costPieData.reduce((pre, now) => now.y + pre, 0).toFixed(2)}`,
    format: (val: number) => {
      return `&yen ${val.toFixed(2)}`;
    },
  }
  stvisible = false

  para = { aa: 1 }
  CKC009 = null
  params = {
    query: "JZCX_query",
    summary: 'ybcx_xmhz',
    cost: 'ybcx_fyxq',
    fund: 'ybcx_jjfl',
    CostClassification: 'ybcx_fyfl',
    disease: 'ybcx_jbfl',
    save:'MXCX_save',
    conditionQuery:'MXCX_query',
    delCondition:'MXCX_del',
    share:'MXCX_share',
    form: {
      'CKC009': this.CKC009
    }
  }
  isVisible = false

  columns3: STColumn[] = [
    { title: '基金分类', index: 'BKA157', dic: 'BKA157' },
    { title: '基金金额', index: 'AAE019' },
  ]
  columns4: STColumn[] = [
    { title: '费用类别', index: 'AKA005', dic: 'AKA005' },
    { title: '明细类别', index: 'AKA003', dic: 'AKA003' },
    { title: '总金额', type: 'currency', index: 'AKC057' },
    { title: '自理', type: 'currency', index: 'AKC058' },
    { title: '自费', type: 'currency', index: 'AKC059' },
  ]
  columns1: STColumn[] = [
    { title: '中心编码', index: 'AKC053' },
    { title: '名称', index: 'AKC052' },
    { title: '药品诊疗', index: 'AKA003', dic: 'AKA003' },
    { title: '中心名称', index: 'AKE036' },
    { title: '数量', type: 'number', index: 'AKC056' },
    { title: '金额', type: 'currency', index: 'AKC057' },
    { title: '医保范围金额', type: 'currency', index: 'YBFWJE' },
  ]
  columns2: STColumn[] = [
    { title: '中心编码', index: 'AKC053', width: 100 },
    { title: '处方日期', index: 'BKE004', width: 100 },
    { title: '项目名称', index: 'AKC052', width: 200 },
    { title: '医保费用', type: 'currency', index: 'AKC057', width: 100 },
    { title: '数量', type: 'number', index: 'AKC056', width: 100 },
    { title: '规格', index: 'AKE025', width: 100 },
    { title: '单位', index: 'AKE024', width: 100 },
    { title: '目录备注', index: 'AKC202', width: 200 },
    { title: ' 开方数量', index: 'AKC430', type: 'number', width: 100 },
    { title: ' 贴数', index: 'BKC014', type: 'number', width: 100 },
    { title: ' 中心项目名称', index: 'AKE036', width: 100 },
    { title: ' 出院带药', index: 'AKC434', width: 100 },
    { title: ' 单价', index: 'AKC055', type: 'number', width: 100 },
    { title: ' 限价', index: 'AKC228', type: 'number', width: 100 },
    { title: ' 金额', index: 'AKC057', type: 'currency', width: 100 },
    { title: ' 自付比例', index: 'AKB040', width: 100 },
  ]
  columns5: STColumn[] = [
    { title: '疾病序号', index: 'AKC187', dic: 'AKC187' },
    { title: '疾病代码', index: 'AKC188' },
    { title: '疾病名称 ', index: 'AKC189' },
  ]



  @ViewChild('stfund', { static: false }) stfund: GridComponent
  @ViewChild('costst', { static: false }) stcost: GridComponent
  ngOnInit() {
    this.CKC009 = this.record.CKC009
          this.params.form = {
            'CKC009': this.CKC009
          }
          // setTimeout(() => {
          //   this.fundClick();
          // }, 1000);
  }






  // 基金饼状图
  fundClick() {
    this.fundPieData = [];
    this.fundvisible = true
    this.stfund.data.map((d) => {
      const x = d._values[0].text
      const y = d.AAE019
      this.fundPieData.push({ x, y })
    })
    this.fund.total = `&yen ${this.fundPieData.reduce((pre, now) => now.y + pre, 0).toFixed(2)}`
  }

  // 费用饼状图
  // TODO: 添加动态数据
  costClick() {
    this.costPieData =  [
      { value: 251, type: '药品', name: '西药' },
    
      { value: 610, type: '诊疗', name: '治疗费' },
      { value: 434, type: '诊疗', name: '手术费' },
      { value: 335, type: '材料', name: '当归' },
      { value: 250, type: '材料', name: '燕窝' },

    ];
      const d = this.stcost.data.map((da)=>{
        return {
          value:da.AKC057,
          type:da._values[1].text,
          name:da._values[0].text,
        }
      })
      this.costPieData = d;
   
    setTimeout(()=>{
        this.costvisible = true;
    },1000) 
  }
  DeselectFund() {
    // 避免二次检测
    setTimeout(() => {
      this.fundvisible = false;
    }, 100);

  }
  DeselectCost() {
    // 避免二次检测
    setTimeout(() => {
      this.costvisible = false;
    }, 100);

  }

}
